<template>
  <div>
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item label="商品名称">
        <el-input v-model="form.goodsName"/>
      </el-form-item>

      <el-form-item label="商品简介">
        <el-input v-model="form.goodsDisc" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="是否为礼品" prop="isCanLeave">
        <el-select v-model="form.goodsType" placeholder="请选择是否为礼品">
          <el-option :value="1" label="是"/>
          <el-option :value="0" label="否"/>
        </el-select>
      </el-form-item>
      <el-form-item label="商品价格" v-show="!form.goodsType">
        <el-input-number :min="1" v-model="form.goodsPrice"/>
      </el-form-item>
      <el-form-item label="兑换积分" v-show="form.goodsType">
        <el-input-number :min="0" v-model="form.scores"/>
      </el-form-item>

      <el-form-item label="商品库存">
        <el-input-number :min="1" v-model="form.goodsNumber"/>
      </el-form-item>

      <el-form-item>
        <el-upload
            :before-remove="beforeRemove"
            :file-list="fileList"
            :http-request="handleUpload"
            :limit="1"
            :on-exceed="handleExceed"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            action
            class="upload-demo"
            list-type="picture-card"
            multiple
            name="singleFile"
        >
          <i class="el-icon-plus"/>
          <div slot="tip" class="el-upload__tip">最多上传一张商品主图</div>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit(form)">立即发布</el-button>
        <el-button @click="handleCancel(`form`)">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        goodsName: "",
        goodsDisc: "",
        goodsPrice: "",
        goodsNumber: "",
        goodsImage: "",
        goodsType: 0,
        scores: 0,
      },
      fileList: [],
      realFileUrl: [],
    };
  },
  methods: {
    handleCancel () {
      this.form = {
        goodsName: "",
        goodsDisc: "",
        goodsPrice: "",
        goodsNumber: "",
        goodsImage: "",
        goodsType: 0,
        scores: 0,
      };
    },
    handleRemove (file, fileList) {
      // console.log(file, fileList)
    },
    handlePreview (file) {
      // console.log(file)
    },
    handleExceed (files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove (file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    // 上传文件
    async handleUpload (file) {

      const params = new FormData();
      params.append("files", file.file);
      const res = await this.$http.uploadFile(params);

      if (res.code !== 200) return this.$message.error(res.message);
      this.realFileUrl.push(res.files[0]);
      this.form.goodsImage = res.files[0];
    },

    // 立即发布
    async onSubmit (form) {
      // if ([...form].some(value=>value)) return this.$message.warning("请填写完整相关信息");
      for (let i in form) if (form[i] === "") return this.$message.warning("请补充完相关信息");
      const res = await this.$http.goods_releasegoods(form);

      if (res.code !== 0) return null;
      // this.$router.go(0);
    },
  },
};
</script>

<style lang="less" scoped>
.el-form {
  margin-top: 20px;
}
</style>
